package com.one.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.one.Mapper.CommentMapper;
import com.one.Mapper.OrdersMapper;
import com.one.Mapper.UserMapper;
import com.one.bean.Comment;
import com.one.bean.Orders;
import com.one.bean.Product;
import com.one.bean.User;
import com.one.service.CommentService;
import com.one.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: tzy
 * @create: 2022/1/19
 * @FileName: CommentServiceImpl
 * @Description: 评论业务接口实现类
 */
@Service
@Slf4j
public class CommentServiceImpl implements CommentService {
    @Autowired(required = false)
    private CommentMapper commentMapper;
    @Autowired(required = false)
    private OrdersMapper ordersMapper;
    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * @author: tzy
     * @date: 2022/1/19
     * @return: int
     * @Description: 实现添加评论
     */
    @Override
    public int addComment(String commentImage,String content,Integer ordersId) {


        //查询用户该订单的状态，是否有权限进行评论
        Orders orders0 = new Orders();
        orders0 = ordersMapper.selectById(ordersId);
        if (orders0.getStatus() == 4 || orders0.getStatus() == 5){
            //订单状态为待支付或者已完成，有权限

            //查询comment表中是否已有用户的主评论
            //创建查询条件
            QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
            commentQueryWrapper.eq("orders_id",ordersId).eq("secondary_id",0);
            if (null != commentMapper.selectOne(commentQueryWrapper)){
                //已有主评论，添加追评,创建评论实体
                Comment additionalComment = new Comment();
                additionalComment.setContent(content);
                additionalComment.setCommentImage(commentImage);
                additionalComment.setOrdersId(ordersId);
                additionalComment.setCreateDate(new Date());
                additionalComment.setUpdateDate(new Date());
                additionalComment.setSecondaryId(commentMapper.selectOne(commentQueryWrapper).getId());
                commentMapper.insert(additionalComment);
                return 1;
            }else {
                //没有主评论，添加主评，判断是否需要增加积分
                if (null != ordersMapper.selectById(ordersId).getCommentIntegral() && 0 != ordersMapper.selectById(ordersId).getCommentIntegral()){
                    //评论积分不为0，不增加积分
                    Comment comment = new Comment();
                    comment.setContent(content);
                    comment.setCommentImage(commentImage);
                    comment.setOrdersId(ordersId);
                    comment.setCreateDate(new Date());
                    comment.setUpdateDate(new Date());
                    comment.setSecondaryId(0);
                    commentMapper.insert(comment);
                    return 2;
                }else {
                    //评论积分为0，第一次评论，增加积分
                    Comment comment = new Comment();
                    comment.setContent(content);
                    comment.setCommentImage(commentImage);
                    comment.setOrdersId(ordersId);
                    comment.setCreateDate(new Date());
                    comment.setUpdateDate(new Date());
                    comment.setSecondaryId(0);
                    commentMapper.insert(comment);

                    //增加订单中的评论积分
                    Orders orders = new Orders();
                    orders.setCommentIntegral(10);
                    orders.setId(ordersId);
                    orders.setStatus(5);
                    ordersMapper.updateById(orders);

                    //增加用户的积分
                    User user = new User();
                    user.setId(ordersMapper.selectById(ordersId).getUserId());
                    int integral = userMapper.selectById(user.getId()).getIntegral();
                    int integral2 = integral + 10 ;
                    user.setIntegral(integral2);
                    userMapper.updateById(user);

                    return 3;
                }
            }
        }

        return 4;

    }

    /**
     * @author: tzy
     * @date: 2022/1/19
     * @return: int
     * @Description: 实现删除评论
     */
    @Override
    public int removeComment(Integer commentId) {
        //判断是否存在此评论
        Comment comment = commentMapper.selectById(commentId);
        if (comment != null){
            //判断删除的评论是否为主评论
            if (commentMapper.selectById(commentId).getSecondaryId() == 0){
                //是主评论，追加评论一并删除
                //查询comment表中主评论的追评id集合
                //创建查询条件
                QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("secondary_id",commentId);
                List<Integer> ids = commentMapper.selectList(queryWrapper).stream().map(Comment::getId).collect(Collectors.toList());
                ids.add(commentId);
                return commentMapper.deleteBatchIds(ids);
            }

            //不是主评论，根据id删除追评即可
            return commentMapper.deleteById(commentId);
        }
        return 0;
    }


    /**
     * @author: tzy
     * @date: 2022/1/21
     * @return: com.one.bean.User
     * @Description: 查询对应订单的用户信息
     */
    @Override
    public User findUser(Integer ordersId) {
        //根据订单编号查询用户编号，根据用户编号查询用户详情
        return userMapper.selectById(ordersMapper.selectById(ordersId).getUserId());
    }
}
